package com.card323.crpg.io.swing;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.text.AttributedString;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JPanel;

public class FadingPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4196105041966091906L;
	ArrayList<BufferedImageSuit> img_suit_list = new ArrayList<BufferedImageSuit>();
	private ImageSFXs imageSfx;
	private volatile Object lock =new Object();

	// private int _x;
	// private int _y;

	/**
	 * Create the panel.
	 */

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Iterator<BufferedImageSuit> itor = img_suit_list.iterator();
		while (itor.hasNext()) {
			BufferedImageSuit img_suit = itor.next();
			Graphics2D g2d = (Graphics2D) g;
			float alpha = 1.0f - (img_suit._counter / 20.0f);
			imageSfx.drawFadedImage(g2d, img_suit._buffered_img, img_suit._x,
					img_suit._y + img_suit._counter, alpha);
			img_suit._counter = img_suit._counter + 1;
			if (img_suit._counter == 20) {
				itor.remove();
			}
		}
	}

	public void p_msg(String msg, Color color, int x, int y) {

		if (!img_suit_list.isEmpty()) 
		{
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		BufferedImageSuit img_suit = new BufferedImageSuit();

		img_suit._x = x;
		img_suit._y = y;
		img_suit._counter = 0;
		img_suit._buffered_img = new BufferedImage(30, 20,
				BufferedImage.TYPE_4BYTE_ABGR);
		Graphics2D g2 = (Graphics2D) img_suit._buffered_img.getGraphics();
		g2.setBackground(Color.WHITE);
		g2.setPaint(color);
		AttributedString as = new AttributedString(msg);
		Font bf = new Font("宋体", Font.BOLD, 12);
		as.addAttribute(TextAttribute.FONT, bf);
		g2.drawString(as.getIterator(), 0, 10);
		set_alpha(img_suit._buffered_img);
		img_suit_list.add(img_suit);
		
		new Thread(new Runnable() {
			public void run() {
				synchronized (lock) {
					while (!img_suit_list.isEmpty()) {
						repaint();
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}

			}
		}).start();

	}

	public FadingPanel() {

		//setPreferredSize(new Dimension(800, 600));
		imageSfx = new ImageSFXs();

	}

	private void set_alpha(BufferedImage bufferedImage) {
		int alpha = 0;
		for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
			for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage
					.getWidth(); j2++) {
				int rgb = bufferedImage.getRGB(j2, j1);

				int R = (rgb & 0xff0000) >> 16;
				int G = (rgb & 0xff00) >> 8;
				int B = (rgb & 0xff);
				if (((255 - R) < 30) && ((255 - G) < 30) && ((255 - B) < 30)) {
					rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
				}

				bufferedImage.setRGB(j2, j1, rgb);

			}
		}

	}

}
